package com.medicinemanager.sql;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.medicinemanager.bean.Entity;
import com.medicinemanager.bean.Staff;

public class StaffManager implements TableManager {
	private SQLiteDatabase mSQLiteDatabase = null;

	public StaffManager(Context context) {
		mSQLiteDatabase = new SQLManager(context).getWritableDatabase();
	}

	@Override
	public Cursor search(String... strings) {
		Cursor cursor = null;
		try {
			cursor = mSQLiteDatabase.rawQuery(
					"select * from staff where account = ? and password = ?",
					strings);
		} catch (SQLException e) {
		}
		return cursor;
	}

	@Override
	public boolean insert(Entity entity) {
		Staff staff = (Staff) entity;
		mSQLiteDatabase.beginTransaction();
		try {
			mSQLiteDatabase.execSQL(
					"insert into staff values(?, ?, ?, ?, ?, ?, ?, ?)",
					new Object[] { staff.getId(), staff.getAccount(),
							staff.getPassword(), staff.getName(),
							staff.getSex(), staff.getAge(),
							staff.getEducation(), staff.getJob() });
			mSQLiteDatabase.setTransactionSuccessful();
		} catch (SQLException e) {
		} finally {
			mSQLiteDatabase.endTransaction();
		}
		return false;
	}

	@Override
	public boolean delete(Entity entity) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(Entity entity) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public ArrayList<Entity> rawQuery(String string) {
		ArrayList<Entity> result = null;
		try{
			result = new ArrayList<Entity>();
			Cursor cursor = mSQLiteDatabase.rawQuery(string, null);
			while(cursor.moveToNext()){
				Staff staff = new Staff();
				staff.setAccount(cursor.getString(cursor.getColumnIndex("account")));
				staff.setAge(cursor.getInt(cursor.getColumnIndex("age")));
				staff.setEducation(cursor.getString(cursor.getColumnIndex("education")));
				staff.setId(cursor.getString(cursor.getColumnIndex("id")));
				staff.setJob(cursor.getString(cursor.getColumnIndex("job")));
				staff.setName(cursor.getString(cursor.getColumnIndex("name")));
				staff.setPassword(cursor.getString(cursor.getColumnIndex("password")));
				staff.setSex(cursor.getString(cursor.getColumnIndex("sex")));
				result.add(staff);
			}
		}catch(SQLException e){
			
		}
		return result;
	}

	@Override
	public boolean execSQL(String... strings) {
		// TODO Auto-generated method stub
		return false;
	}

}
